<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/3/10
 * Time: 14:26
 */

namespace App\Http\Logics\MiniProgram;


use App\PartyContent;
use App\Http\Logics\CommonLogic;
use App\Http\Logics\BaseLogic;
class PartyContentLogic extends BaseLogic
{
    public static function updatePartyContent($data)
    {
        // 将之前的删除
        PartyContent::where('party_id' ,$data['party_id'])->delete();

        errorLog($data);
        $content = json_decode($data['content_str']);
        foreach ($content as $key => $item){
            $party_content = new PartyContent();
            // 公众参数
            $party_content->party_id = $data['party_id'];
            $party_content->sort = $key;
            $response = CommonLogic::handleContent($party_content, $item);
            if ($response->code){
                return self::response($response->code, $response->data);
            }
        }
        return self::response(self::SUCCESS);
    }

    public static function getPartyContentDetail($data)
    {
        $party_content = PartyContent::where(function ($query) use ($data){
            $query->where('party_id', $data['party_id']);
        })->orderBy('sort')->get();
        if (empty($party_content)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $party_content);
    }
}